package com.kiss.fw.dao.handlers;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.sql.Date;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

public class CalendarTypeHandler implements TypeHandler {
	@Override
	public Object getResult(ResultSet _resultSet, String _columnName) throws SQLException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(_resultSet.getDate(_columnName));
		
		return calendar;
	}
	
	@Override
	public Object getResult(CallableStatement _callableStatement, int _columnIndex) throws SQLException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(_callableStatement.getDate(_columnIndex));
		
		return calendar;
	}
	
	@Override
	public void setParameter(PreparedStatement _preparedStatement, int _arg1, Object _object, JdbcType _jdbcType) throws SQLException {
		if(JdbcType.DATE == _jdbcType) {
			// TODO: Check if required, log _jdbcType
		}
		
		Calendar calendar = (Calendar)_object;
		Date date = new Date(calendar.getTimeInMillis());
		
		_preparedStatement.setDate(_arg1, date);
	}
}